<?php

namespace App\Console\Commands;

use App\Model\Branch;
use App\Model\Order;
use App\Model\PushOrder;
use App\Model\tel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ryhfPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ryhfPush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                //控制是否推单给下线
//                $branch = Branch::where("id", 12)->first();
//                if ($branch['tel'] == 1) {
                if (true) {
                    $order = DB::select("select id,buytime,chargeaccount,price,`order`,`type` from tiktok_tel where isok = 0 order by id asc limit 1");
//                    $num = Branch::where("tel", 1)->count();
                    if (isset($order[0])) {
                        $order1 = $order[0];
                        $order = (array)$order1;
//                        if ($order['id'] % $num == 0) {
                        if (true) {
                            $res = $this->ry_PushOrder($order['chargeaccount'], $order['order'], $order['price'],$order['type']);
                            if($res == "noProduct"){
                                $info['isok'] = 2;
                                $info['status'] = '已处理';
                                $info['branch'] = '充值失败';
                                tel::where("order", $order['order'])->update($info);
                                continue;
                            }
                            $info['callback'] = $res;
                            tel::where("order", $order['order'])->update($info);
//                        Order1::where("orderno",$order['orderno'])->update($info);
                            $res = json_decode($res);
                            if ($res->status == 200) {
                                $info['status'] = '已处理';
                                $info['branch'] = '已推送,等待反馈';
                                $info['isok'] = 3;
                                $info['pid'] = 12;
                                $info['time'] = time();
                                tel::where("order", $order['order'])->update($info);
                            }elseif ($res->status == 500){
                                $infoFulu['branch'] = "充值失败";
                                $infoFulu['status'] = '已处理';
                                $infoFulu['isok'] = 2;
                                $infoFulu['bid'] = 12;
                            }else{
                                $infoFulu['branch'] = $res->message;
                                $infoFulu['status'] = '已处理';
                                $infoFulu['isok'] = 2;
                                $infoFulu['bid'] = 12;
                            }
                        }
                    }
                }
            } catch (\Exception $e) {
                echo $e->getMessage().'----'.$e->getLine();
            }
            sleep(2);
        }
    }
    //软游   推送方法
    public function ry_PushOrder($dyid, $orderid, $money,$type, $num = 1)
    {
        switch ($money) {
            case 50:
                $goodsCode = 'goods_243';
                break;
            case 100:
                $goodsCode = 'goods_244';
                break;
            case 200:
                $goodsCode = 'goods_245';
                break;
            default:
                return "noProduct";
        }
        /***低价start**/
//        if($type == 1){
//            switch ($money) {
//                case 50:
//                    $goodsCode = 'goods_243';
//                    break;
//                case 100:
//                    $goodsCode = 'goods_244';
//                    break;
//                case 200:
//                    $goodsCode = 'goods_245';
//                    break;
//                default:
//                    return "noProduct";
//            }
//        }elseif($type == 2){
//            switch ($money) {
//                case 50:
//                    $goodsCode = 'goods_243';
//                    break;
//                case 100:
//                    $goodsCode = 'goods_244';
//                    break;
//                case 200:
//                    $goodsCode = 'goods_245';
//                    break;
//                default:
//                    return "noProduct";
//            }
//        }else{
//            switch ($money) {
//                case 50:
//                    $goodsCode = 'goods_243';
//                    break;
//                case 100:
//                    $goodsCode = 'goods_244';
//                    break;
//                case 200:
//                    $goodsCode = 'goods_245';
//                    break;
//                default:
//                    return "noProduct";
//
//            }
//        }
        /***end**/
        $time = time() . rand(100, 999);
        $callBackUrl = 'http://112.124.24.221/admin/callback/ry_AutoConfirmHfOrder';
        $sign = md5('zmypcennn9businessCode=business_21businessOrderId=' . $orderid . 'callBackUrl=' . $callBackUrl . 'chargeCode=' . $dyid . 'goodsCode=' . $goodsCode . 'num=' . $num . 'timeStamp=' . $time . 'tradeIp=127.0.0.1zmypcennn9');
        $post = [
            'businessCode' => 'business_21',
            'businessOrderId' => $orderid,
            'callBackUrl' => $callBackUrl,
            'chargeCode' => $dyid,
            'goodsCode' => $goodsCode,
            'num' => 1,
            'sign' => $sign,
            'timeStamp' => $time,
            'tradeIp' => '127.0.0.1'
        ];
        $json_post = json_encode($post);
        return $this->ry_curl_post_https('http://api.rytjsj.com:60000/business-center/order/sendOrder', $json_post);
    }

    function ry_curl_post_https($url, $data)
    { // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
//        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据，json格式
    }

}
